package com.literature.threeGroup.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @Name: SystemIDUtil
 * @Description
 * @author: dongzp
 * @date: 2019/12/05
 * @Version: 1.0
 **/
public class SystemIDUtil {
    public static final String RJ_PREFIX = "RJ";//期刊
    public static final String RB_PREFIX = "RB";//非刊
    public static final String OJ_PREFIX = "OJ";//期刊订单
    public static final String OB_PREFIX = "OB";//非刊订单
    public static final String CJ_PREFIX = "CJ";//期刊套
    public static final String CB_PREFIX = "CB";//非刊套
    public static final String OCJ_PREFIX = "OCJ";//期刊套订单
    public static final String OCB_PREFIX = "OCB";//非刊套订单
    public static final String TYPE = "0";//期刊
    public static final String NOTYPE = "1";//非刊

    public static void main(String[] args) {
        System.out.println(SystemIDUtil.getResourceId("01830c7a1a3834b70513477d2295e3b4", 0));
        System.out.println(SystemIDUtil.getResourceId("01830c7a1a3834b70513477d2295e3b4", 0));
        System.out.println(SystemIDUtil.getResourceId("0176242ee91e5ea078d5b213fe0361a8", 1));
        System.out.println(SystemIDUtil.getResourceOrderId("000013e343a699d8b5eafa3114b4d3e6", 0));
        System.out.println(SystemIDUtil.getResourceOrderId("000231f88e7df10011cf2871427ca956", 1));
        System.out.println(SystemIDUtil.getCoverId("0002dced4b0f56c477ee841d7bcd1f8d", 0));
        System.out.println(SystemIDUtil.getCoverId("0003995a1c10c305d38dc44ad1f62d3a", 1));
        System.out.println(SystemIDUtil.getCoverOrderId("00077e582387f565261223b2eec3a8a6", 0));
        System.out.println(SystemIDUtil.getCoverOrderId("00091f58dae534069b07e348301adbf0", 1));
    }


    public static String getOCJ(){
        return SystemIDUtil.getCoverOrderId(UUID.randomUUID().toString(), 0);
    }
    public static String getOCB(){
        return SystemIDUtil.getCoverOrderId(UUID.randomUUID().toString(), 1);
    }

    /**
     * get timestamp
     */
    private static String getDateTime(String format) {
        DateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date());
    }

    /**
     * get hashcode from system id
     */
    private static String getHashCode(String systemId) {
        int hash = systemId.hashCode();
        if (hash < 0)
            hash = -hash;
        return String.format("%010d", hash);
    }

    private static synchronized String getLongCode(String systemId) {
        return getDateTime("yyyyMMdd") + getHashCode(systemId);
    }

    private static synchronized String getMidCode(String systemId) {
        return getDateTime("yyyy") + getHashCode(systemId);
    }

    private static synchronized String getShortCode(String systemId) {
        return String.valueOf(getHashCode(systemId));
    }

    /**
     * get resource id
     */
    public static String getResourceId(String systemId, int type) {
        String result = null;
        if (type == 0) {
            result = RJ_PREFIX + getShortCode(systemId);
        } else {
            result = RB_PREFIX + getShortCode(systemId);
        }
        return result;
    }

    /**
     * get resource order id
     */
    public static String getResourceOrderId(String systemId, int type) {
        String result = null;
        if (type == 0) {
            result = OJ_PREFIX + getLongCode(systemId);
        } else {
            result = OB_PREFIX + getLongCode(systemId);
        }
        return result;
    }

    /**
     * get cover id
     */
    public static String getCoverId(String systemId, int type) {
        String result = null;
        if (type == 0) {
            result = CJ_PREFIX + getShortCode(systemId);
        } else {
            result = CB_PREFIX + getShortCode(systemId);
        }
        return result;
    }

    /**
     * get cover order id
     */
    public static String getCoverOrderId(String systemId, int type) {
        String result = null;
        if (type == 0) {
            result = OCJ_PREFIX + getMidCode(systemId);
        } else {
            result = OCB_PREFIX + getMidCode(systemId);
        }
        return result;
    }

}